﻿using System;
using Benchmark.Core.Communication.Data.Interface;
using Benchmark.Core.Communication.Data.Serialization;
using Benchmark.Core.Communication.Hub.Impl.Pipe.Core;

namespace Benchmark.Core.Communication.Hub.Impl.Pipe
{
    public class AsyncPipeClientEventArgument : PipeClient
    {

       // IValidationSet _validate;
        readonly IFormatter _format;
        public delegate void OnPreExecution();
        public delegate void OnPostExecution();
        OnPreExecution _onPreOperationStart;

        public OnPreExecution OnPreOperationStartDelegate
        {
            get { return _onPreOperationStart; }
            set { _onPreOperationStart = value; }
        }

        OnPostExecution _onPostOperationStart;

        public OnPostExecution OnPostOperationStartDelegate
        {
            get { return _onPostOperationStart; }
            set { _onPostOperationStart = value; }
        }


        public AsyncPipeClientEventArgument()
        {
            _format = new FormatterBinary();
        }



        public void ManageResponse<TPipeOutType>(TPipeOutType response)
        {

            if (OnPreOperationStartDelegate != null)
                OnPreOperationStartDelegate.Invoke();
            byte[] bytesArray = _format.SerializeToByte<TPipeOutType>(response);
               base.Write(bytesArray);
            if (OnPostOperationStartDelegate != null)
                OnPostOperationStartDelegate.Invoke();
        }


        public  TPipeInType ReadFromPipe<TPipeInType>()
        {
            if(OnPreOperationStartDelegate!=null)
            OnPreOperationStartDelegate.Invoke();

            byte[] bytesArray = base.Read();
                 var decodedString = _format.DeserializeFromBytes<TPipeInType>(bytesArray);
                 if (OnPostOperationStartDelegate != null)
                 OnPostOperationStartDelegate.Invoke();
        return(decodedString);

        }

       


}
}
